package controller

import (
	"gin/common"
	"gin/response"
	"gin/service"
	"gin/validate"
	"github.com/gin-gonic/gin"
)

func WordSimilarSelect(ctx *gin.Context) {
	var selectDictQuery validate.SelectWordSimilarQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	total, resData, queryErr := service.SelectWordSimilar(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": gin.H{"total": total, "rows": resData}}, "success")
	}
}

// 查询单条词库
func WordSimilarFind(ctx *gin.Context) {
	var selectDictQuery validate.SelectWordSimilarQuery

	err := ctx.ShouldBindJSON(&selectDictQuery)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateSelectQueryParam(selectDictQuery.Param, "log")

	resData, queryErr := service.FindWordSimilar(selectQueryParam)

	if queryErr != nil {
		// 查询失败
		response.Fail(ctx, nil, queryErr.Error())
	} else {
		// 查询成功
		response.Success(ctx, gin.H{"data": resData}, "success")
	}
}

// 插入日志
func WordSimilarCreate(ctx *gin.Context) {
	var addWordSimilar validate.AddWordSimilarWrapper

	err := ctx.ShouldBindJSON(&addWordSimilar)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.CreateWordSimilar(addWordSimilar.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	}

	response.Success(ctx, gin.H{"data": resData}, "success")
}

func WordSimilarUpdate(ctx *gin.Context) {
	var updateWordSimilar validate.UpdateWordSimilarWrapper

	err := ctx.ShouldBindJSON(&updateWordSimilar)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	resData, createErr := service.UpdateWordSimilar(updateWordSimilar.Data)
	if createErr != nil {
		// 插入失败
		response.Fail(ctx, nil, createErr.Error())
		return
	} else {
		// 插入数据成功
		updateWordSimilar.Data.Id = resData.Id
		updateWordSimilar.Data.Created = resData.Created
	}

	response.Success(ctx, gin.H{"data": updateWordSimilar.Data}, "success")
}

func WordSimilarDelete(ctx *gin.Context) {
	var deleteWordSimilarParam validate.DeleteWordSimilarParam

	err := ctx.ShouldBindJSON(&deleteWordSimilarParam)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	selectQueryParam := common.GenerateFilterParam(&deleteWordSimilarParam.Param, "sentence_word")

	service.DeleteWordSimilar(&selectQueryParam)

	// 删除成功
	response.Success(ctx, nil, "success")

}

func WordSimilarQuerySimilarWord(ctx *gin.Context) {
	var queryWordSimilarParam validate.QuerySimilarWordWithAnalysis

	err := ctx.ShouldBindJSON(&queryWordSimilarParam)

	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}

	_, res, err := service.QueryWordSimilarSimilarWords(&queryWordSimilarParam)
	if err != nil {
		response.Fail(ctx, nil, err.Error())
		return
	}
	var resp response.WordSimilarQuerySimilarWordResp
	resp.Word = queryWordSimilarParam.Word
	resp.SimilarWords = make([]string, len(res))
	for index, row := range res {
		resp.SimilarWords[index] = row.Word
	}
	response.Success(ctx, gin.H{"data": resp}, "success")
	return
}
